import { request } from '../axios/axios'

/**
 * 用户认证相关API
 */

// 用户登录
export function login(loginData) {
  return request.post('/api/auth/login', loginData)
}

// 获取用户信息
export function getUserInfo() {
  return request.get('/api/auth/userInfo')
}

// 获取用户菜单权限
export function getUserMenus() {
  return request.get('/api/auth/menus')
}

// 获取用户权限列表
export function getUserPermissions() {
  return request.get('/api/auth/permissions')
}

// 用户登出
export function logout() {
  return request.post('/api/auth/logout')
}

// 刷新token
export function refreshToken() {
  return request.post('/api/auth/refresh')
}

// 验证token有效性
export function validateToken() {
  return request.get('/api/auth/validate')
}

// 修改密码
export function changePassword(passwordData) {
  return request.post('/api/auth/changePassword', passwordData)
}

/**
 * 模拟数据 - 用于开发阶段
 * 在实际部署时请注释掉这部分代码
 */

// 模拟用户数据
const mockUsers = {
  admin: {
    id: 1,
    username: 'admin',
    name: '管理员',
    role: 'admin',
    avatar: 'https://avatars.githubusercontent.com/u/1?v=4',
    email: 'admin@example.com',
    phone: '13800138000',
    permissions: ['*'], // 通配符表示所有权限
    token: 'admin-token-' + Date.now()
  },
  editor: {
    id: 2,
    username: 'editor',
    name: '编辑员',
    role: 'editor',
    avatar: 'https://avatars.githubusercontent.com/u/2?v=4',
    email: 'editor@example.com',
    phone: '13800138001',
    permissions: [
      'dashboard:view',
      'user:view', 'user:create', 'user:edit',
      'content:view', 'content:create', 'content:edit',
      'profile:view', 'profile:edit'
    ],
    token: 'editor-token-' + Date.now()
  },
  user: {
    id: 3,
    username: 'user',
    name: '普通用户',
    role: 'user',
    avatar: 'https://avatars.githubusercontent.com/u/3?v=4',
    email: 'user@example.com',
    phone: '13800138002',
    permissions: [
      'dashboard:view',
      'profile:view', 'profile:edit'
    ],
    token: 'user-token-' + Date.now()
  }
}

// 模拟菜单数据 - 根据角色返回不同菜单
const mockMenus = {
  admin: [
    {
      id: 1,
      path: '/dashboard',
      name: 'Dashboard',
      title: '仪表盘',
      icon: 'Odometer',
      component: 'Dashboard',
      meta: {
        title: '仪表盘',
        requiresAuth: true,
        permissions: ['dashboard:view']
      }
    },
    {
      id: 2,
      path: '/system',
      name: 'System',
      title: '系统管理',
      icon: 'Setting',
      meta: {
        title: '系统管理',
        requiresAuth: true
      },
      children: [
        {
          id: 21,
          path: '/system/user',
          name: 'SystemUser',
          title: '用户管理',
          icon: 'User',
          component: 'SystemUser',
          meta: {
            title: '用户管理',
            requiresAuth: true,
            permissions: ['user:view']
          }
        },
        {
          id: 22,
          path: '/system/role',
          name: 'SystemRole',
          title: '角色管理',
          icon: 'UserFilled',
          component: 'SystemRole',
          meta: {
            title: '角色管理',
            requiresAuth: true,
            permissions: ['role:view']
          }
        },
        {
          id: 23,
          path: '/system/menu',
          name: 'SystemMenu',
          title: '菜单管理',
          icon: 'Menu',
          component: 'SystemMenu',
          meta: {
            title: '菜单管理',
            requiresAuth: true,
            permissions: ['menu:view']
          }
        }
      ]
    },
    {
      id: 3,
      path: '/content',
      name: 'Content',
      title: '内容管理',
      icon: 'Document',
      meta: {
        title: '内容管理',
        requiresAuth: true
      },
      children: [
        {
          id: 31,
          path: '/content/article',
          name: 'ContentArticle',
          title: '文章管理',
          icon: 'EditPen',
          component: 'ContentArticle',
          meta: {
            title: '文章管理',
            requiresAuth: true,
            permissions: ['content:view']
          }
        },
        {
          id: 32,
          path: '/content/category',
          name: 'ContentCategory',
          title: '分类管理',
          icon: 'Collection',
          component: 'ContentCategory',
          meta: {
            title: '分类管理',
            requiresAuth: true,
            permissions: ['content:view']
          }
        }
      ]
    },
    {
      id: 4,
      path: '/profile',
      name: 'Profile',
      title: '个人中心',
      icon: 'Avatar',
      component: 'Profile',
      meta: {
        title: '个人中心',
        requiresAuth: true,
        permissions: ['profile:view']
      }
    },
    {
      id: 5,
      path: '/settings',
      name: 'Settings',
      title: '系统设置',
      icon: 'Tools',
      component: 'Settings',
      meta: {
        title: '系统设置',
        requiresAuth: true,
        permissions: ['system:settings']
      }
    }
  ],
  editor: [
    {
      id: 1,
      path: '/dashboard',
      name: 'Dashboard',
      title: '仪表盘',
      icon: 'Odometer',
      component: 'Dashboard',
      meta: {
        title: '仪表盘',
        requiresAuth: true,
        permissions: ['dashboard:view']
      }
    },
    {
      id: 4,
      path: '/profile',
      name: 'Profile',
      title: '个人中心',
      icon: 'Avatar',
      component: 'Profile',
      meta: {
        title: '个人中心',
        requiresAuth: true,
        permissions: ['profile:view']
      }
    }
  ],
  user: [
    {
      id: 1,
      path: '/dashboard',
      name: 'Dashboard',
      title: '仪表盘',
      icon: 'Odometer',
      component: 'Dashboard',
      meta: {
        title: '仪表盘',
        requiresAuth: true,
        permissions: ['dashboard:view']
      }
    },
    {
      id: 4,
      path: '/profile',
      name: 'Profile',
      title: '个人中心',
      icon: 'Avatar',
      component: 'Profile',
      meta: {
        title: '个人中心',
        requiresAuth: true,
        permissions: ['profile:view']
      }
    }
  ]
}

/**
 * 模拟登录 - 开发阶段使用
 */
export function mockLogin(loginData) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const { username, password } = loginData
      const user = mockUsers[username]
      
      if (user && password === '123456') {
        resolve({
          code: 200,
          message: '登录成功',
          data: {
            token: user.token,
            userInfo: {
              id: user.id,
              username: user.username,
              name: user.name,
              role: user.role,
              avatar: user.avatar,
              email: user.email,
              phone: user.phone
            },
            permissions: user.permissions
          }
        })
      } else {
        reject(new Error('用户名或密码错误'))
      }
    }, 1000) // 模拟网络延迟
  })
}

/**
 * 模拟获取菜单 - 开发阶段使用
 */
export function mockGetUserMenus(role) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const menus = mockMenus[role] || []
      resolve({
        code: 200,
        message: '获取成功',
        data: menus
      })
    }, 500)
  })
}

/**
 * 模拟获取用户信息 - 开发阶段使用
 */
export function mockGetUserInfo(token) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      // 根据token查找用户
      const user = Object.values(mockUsers).find(u => u.token === token)
      
      if (user) {
        resolve({
          code: 200,
          message: '获取成功',
          data: {
            id: user.id,
            username: user.username,
            name: user.name,
            role: user.role,
            avatar: user.avatar,
            email: user.email,
            phone: user.phone,
            permissions: user.permissions
          }
        })
      } else {
        reject(new Error('Token无效或已过期'))
      }
    }, 300)
  })
}